﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.CongNo
{
    public partial class frmCongNo : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Khởi tạo
        bool kiemtra = false;
        bool checkClose = false;
        bool check = false;
        ArrayList list;
        public static string a = "0";
        Entities.KhachHang[] kh;
        Entities.NhaCungCap[] ncc;
        Entities.HDBanLe[] buonle;
        Entities.HDBanBuon[] bb;
        Entities.ChiTietHDBanBuon[] chitiethdbanbuon;
        Entities.CongNo[] hienthi;
        Entities.CongNo[] congnokh;
        Entities.CongNo[] congnoncc;
        Entities.CongNo[] congno;
        Entities.CongNo cn;
        Entities.HoaDonNhap[] hdn;
        Entities.TraLaiNCC[] tl;
        Entities.KhachHangTraLaiBanBuon[] khbb;
        Entities.KhachHangTraLaiBanLe[] khbl;
        Entities.ChiTietPhieuTTCuaKH[] chitietphieuttcuakh;
        Entities.ChiTietPhieuTTNCC[] chitietphieuttncc;
        Entities.PhieuTTCuaKH[] phieuttcuakh;
        Entities.PhieuTTNCC[] phieuttncc;
        public static Entities.SoDuCongNo[] mangchitiet;
        Entities.SoDuCongNo[] hienthi1;
        private string MaKho = string.Empty;
        public string month = "";
        public string year = "";
        string makho = "";
        string tenkho = "";
        public DateTime date;
        DateTime batdau;
        DateTime ketthuc;
        double phatsinhno = 0;
        double phatsinhco = 0;
        double sdck = 0;
        double sddk = 0;
        ArrayList array;
        int i = 0;

        public frmCongNo()
        {
            InitializeComponent();
            try
            {
                lbTrangThai.Visible = false;
                Entities.CaiDatKho kho = Entitystatic.ViewFile();
                makho = kho.MaKho;
                tenkho = kho.TenKho;
                if (makho.Length == 0)
                {
                    MessageBox.Show(Common.MSG.MSG28(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                date = Entitystatic.date;
                cbbNam.Text = date.Year.ToString();
                cbbThang.Text = date.Month.ToString();
                month = cbbThang.Text;
                year = cbbNam.Text;
                batdau = DateTime.Parse(cbbThang.Text + "/01/" + cbbNam.Text);
                if (cbbThang.Text == "12")
                    ketthuc = DateTime.Parse("01/01/" + (Convert.ToInt32(cbbNam.Text) + 1).ToString());
                else
                    ketthuc = DateTime.Parse((Convert.ToInt32(cbbThang.Text) + 1).ToString() + "/01/" + cbbNam.Text);
                cbbDoiTuong.SelectedIndex = 0;
                HienThiTong();
            }
            catch
            {
            }
        }

        public frmCongNo(string nam, string thang, int loaiDoiTuong)
        {
            try
            {
                InitializeComponent();
                Entities.CaiDatKho kho1 = Entitystatic.ViewFile();
                makho = kho1.MaKho;
                tenkho = kho1.TenKho;
                if (makho.Length == 0)
                {
                    MessageBox.Show(Common.MSG.MSG28(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                cbbDoiTuong.SelectedIndex = loaiDoiTuong;
                month = thang;
                year = nam;
                date = DateTime.Parse(thang + "/01/" + nam);
                cbbNam.Text = nam;
                cbbThang.Text = thang;
                batdau = DateTime.Parse(cbbThang.Text + "/01/" + cbbNam.Text);
                if (cbbThang.Text == "12")
                    ketthuc = DateTime.Parse("01/01/" + (Convert.ToInt32(cbbNam.Text) + 1).ToString());
                else
                    ketthuc = DateTime.Parse((Convert.ToInt32(cbbThang.Text) + 1).ToString() + "/01/" + cbbNam.Text);

                HienThiTong();
            }
            catch
            {
            }
        }

        void SelectAll(string hanhdong)
        {
            Entities.SelectAll selectall = new Entities.SelectAll("CongNo", hanhdong);
            selectall = Common.StaticVariable.InterfaceObject.SelectAll(selectall);
            if (hanhdong == "KhachHang")
            {
                kh = selectall.KhachHang;
                buonle = selectall.HDBanLe;
                bb = selectall.HDBanBuon;
                phieuttcuakh = selectall.PhieuTTCuaKH;
                chitietphieuttcuakh = selectall.ChiTietPhieuTTCuaKH;
            }
            if (hanhdong == "NhaCungCap")
            {
                ncc = selectall.NhaCungCap;
                tl = selectall.TraLaiNCC;
                phieuttncc = selectall.PhieuTTNCC;
                chitietphieuttncc = selectall.ChiTietPhieuTTNCC;
            }

            if (bb == null)
                bb = new Entities.HDBanBuon[0];
            if (chitiethdbanbuon == null)
                chitiethdbanbuon = new Entities.ChiTietHDBanBuon[0];
            if (buonle == null)
                buonle = new Entities.HDBanLe[0];
            if (hdn == null)
                hdn = new Entities.HoaDonNhap[0];
            if (kh == null)
                kh = new Entities.KhachHang[0];
            if (ncc == null)
                ncc = new Entities.NhaCungCap[0];
            if (khbb == null)
                khbb = new Entities.KhachHangTraLaiBanBuon[0];
            if (khbl == null)
                khbl = new Entities.KhachHangTraLaiBanLe[0];
            if (phieuttcuakh == null)
                phieuttcuakh = new Entities.PhieuTTCuaKH[0];
            if (chitietphieuttcuakh == null)
                chitietphieuttcuakh = new Entities.ChiTietPhieuTTCuaKH[0];
            if (phieuttncc == null)
                phieuttncc = new Entities.PhieuTTNCC[0];
            if (chitietphieuttncc == null)
                chitietphieuttncc = new Entities.ChiTietPhieuTTNCC[0];
            if (tl == null)
                tl = new Entities.TraLaiNCC[0];
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.CongNo.MaDoiTuong].Visible = true;
            dgvHienThi.Columns[Common.Constants.CongNo.TenDoiTuong].Visible = true;
            dgvHienThi.Columns[Common.Constants.CongNo.DiaChi].Visible = true;
            dgvHienThi.Columns[Common.Constants.CongNo.SoDuDauKy].Visible = true;
            dgvHienThi.Columns[Common.Constants.CongNo.PhatSinhNo].Visible = true;
            dgvHienThi.Columns[Common.Constants.CongNo.PhatSinhCo].Visible = true;
            dgvHienThi.Columns[Common.Constants.CongNo.SoDuCuoiKy].Visible = true;
            dgvHienThi.Columns[Common.Constants.CongNo.MaDoiTuong].HeaderText = "Mã Đối Tượng";
            dgvHienThi.Columns[Common.Constants.CongNo.TenDoiTuong].HeaderText = "Tên Đối Tượng";
            dgvHienThi.Columns[Common.Constants.CongNo.DiaChi].HeaderText = "Địa Chỉ";
            dgvHienThi.Columns[Common.Constants.CongNo.SoDuDauKy].HeaderText = "Số Dư Đầu Kỳ";
            dgvHienThi.Columns[Common.Constants.CongNo.PhatSinhNo].HeaderText = "Phát Sinh Nợ";
            dgvHienThi.Columns[Common.Constants.CongNo.PhatSinhCo].HeaderText = "Phát Sinh Có";
            dgvHienThi.Columns[Common.Constants.CongNo.SoDuCuoiKy].HeaderText = "Số Dư Cuối Kỳ";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region chi tiết giao dịch
        void ChiTietGiaoDich()
        {
            if (i < 0 || i == ((Entities.CongNo[])dgvHienThi.DataSource).Length - 1)
                return;
            try
            {
                Entities.CongNo[] ad = (Entities.CongNo[])dgvHienThi.DataSource;
                if (i < congnokh.Length - 1)
                {
                    if (cbbDoiTuong.SelectedIndex == 0)
                    {
                        ChiTietTheoKhachHang(ad[i].MaDoiTuong);
                        frmChiTiet a = new frmChiTiet(congnochitietkh, bb1, bl1, phieuttcuakh1, khbb1, khbl1, kh1, ad[i].DuDauKy, ad[i].DuCuoiKy);
                        a.ShowDialog();
                    }
                }
                if (i < congnoncc.Length - 1)
                {
                    if (cbbDoiTuong.SelectedIndex == 1)
                    {
                        ChiTietNhaCungCap(ad[i].MaDoiTuong);
                        frmChiTiet a = new frmChiTiet(congnochitietncc2, hdn2, phieuttncc2, tlncc2, ncc2, ad[i].DuDauKy, ad[i].DuCuoiKy);
                        a.ShowDialog();
                    }
                }
            }
            catch
            {

            }
        }
        #endregion

        #region thoát
        void Thoat()
        {
            this.Close();
        }
        #endregion

        #region Số Dư Công Nợ
        private void SoDuCongNo()
        {
            try
            {
                bool loaiDoiTuong = false;
                if (cbbDoiTuong.SelectedIndex == 0)
                    loaiDoiTuong = false;
                else
                    loaiDoiTuong = true;
                hienthi1 = HienThi(makho);
                Entities.SoDuCongNo sd = new Entities.SoDuCongNo();
                // sd.LoaiDoiTuong = loaiDoiTuong;
                if (hienthi1 == null)
                {
                    hienthi1 = new Entities.SoDuCongNo[0];
                    dgvHienThi.DataSource = hienthi1;
                    fix();
                    return;
                }
                year = cbbNam.Text;
                month = cbbThang.Text;
                int sotang = 0;
                Entities.SoDuCongNo[] sq = new Entities.SoDuCongNo[hienthi1.Length];
                for (int i = 0; i < sq.Length; i++)
                {
                    string namkt = hienthi1[i].NgayKetChuyen.Year.ToString();
                    string thangkt = hienthi1[i].NgayKetChuyen.Month.ToString();

                    if (namkt == year && thangkt == month && hienthi1[i].LoaiDoiTuong == loaiDoiTuong)
                    {
                        sq[sotang] = hienthi1[i];
                        sotang++;
                    }

                }
                if (sotang != 0)
                {
                    hienthi1 = new Entities.SoDuCongNo[sotang];
                    for (int i = 0; i < sotang; i++)
                    {
                        hienthi1[i] = new Entities.SoDuCongNo("", sq[i].MaSoDuCongNo, sq[i].MaDoiTuong, sq[i].TenDoiTuong, sq[i].DiaChi, sq[i].SoDuDauKy);
                    }
                }
                else
                    hienthi1 = new Entities.SoDuCongNo[0];
            }
            catch
            {

            }
        }

        public Entities.SoDuCongNo[] HienThi(string makho)
        {
            try
            {
                Entities.SoDuCongNo[] temp = Common.StaticVariable.InterfaceObject.SelectCongNoTheoKho(makho);
                return temp;
            }
            catch
            {
                return new Entities.SoDuCongNo[0];
            }
        }
        #endregion

        #region Hiển Thị Tổng
        public void HienThiTong()
        {
            try
            {
                list = new ArrayList();
                if (cbbDoiTuong.SelectedIndex == 0)
                {
                    SelectAll("KhachHang");
                    mangchitiet = new Entities.SoDuCongNo[kh.Length];
                }
                else
                {
                    SelectAll("NhaCungCap");
                    mangchitiet = new Entities.SoDuCongNo[ncc.Length];
                }


                hienthi = new Entities.CongNo[bb.Length + buonle.Length + hdn.Length + khbb.Length + khbl.Length + tl.Length + phieuttncc.Length + phieuttcuakh.Length + 1];

                congnokh = new Entities.CongNo[kh.Length + 1];

                congnoncc = new Entities.CongNo[ncc.Length + 1];
                int soluong = 0;
                int soluongkh = 0;
                int soluongncc = 0;
                sdck = sddk = 0;
                Entities.CongNo ht = new Entities.CongNo();
                array = new ArrayList();
                if (cbbDoiTuong.SelectedIndex == 0)
                {
                    #region khách hàng
                    string maSoDuCongNo = "";
                    hienthi1 = HienThi(makho);
                    // Khách Hàng
                    for (int i = 0; i < kh.Length; i++)
                    {
                        phatsinhco = 0;
                        phatsinhno = 0;
                        sddk = 0;
                        foreach (Entities.SoDuCongNo item in hienthi1)
                        {
                            if (item.MaDoiTuong == kh[i].MaKhachHang && batdau.Month == item.NgayKetChuyen.Month && batdau.Year == item.NgayKetChuyen.Year)
                            {
                                maSoDuCongNo = item.MaSoDuCongNo;
                                sddk = Double.Parse(item.SoDuDauKy);
                                break;
                            }
                        }

                        #region bán buôn
                        // bán buôn
                        foreach (Entities.HDBanBuon item in bb)
                        {
                            if (item.MaKho == makho && item.MaKhachHang == kh[i].MaKhachHang && item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc.Date)
                            {
                                phatsinhno += Convert.ToDouble(item.TongTienThanhToan);
                                string ngaylap = Common.Utilities.XuLy(2, item.NgayLap.ToShortDateString());
                                ht = new Entities.CongNo();
                                ht.NgayLap = ngaylap;
                                ht.MaDoiTuong = kh[i].MaKhachHang;
                                ht.TenDoiTuong = kh[i].Ten;
                                ht.DiaChi = kh[i].DiaChi;
                                ht.PhatSinhNo = Format(double.Parse(item.TongTienThanhToan));
                                ht.PhatSinhCo = "0";
                                ht.GhiChu = item.GhiChu;
                                ht.TruongNgam = "BanBuon";
                                ht.MaTruongNgam = item.MaHDBanBuon;
                                array.Add(ht);
                                foreach (Entities.ChiTietPhieuTTCuaKH item1 in chitietphieuttcuakh)
                                {

                                    if (item1.MaHDBanHang == item.MaHDBanBuon)
                                    {
                                        foreach (Entities.PhieuTTCuaKH item2 in phieuttcuakh)
                                        {
                                            if (item2.MaPhieuTTCuaKH == item1.MaPhieuTTCuaKH && item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc.Date)
                                            {
                                                phatsinhco += double.Parse(item1.ThanhToan);
                                                ht = new Entities.CongNo();
                                                ht.NgayLap = item2.NgayLap.ToString("dd/MM/yyyy");
                                                ht.MaDoiTuong = kh[i].MaKhachHang;
                                                ht.TenDoiTuong = kh[i].Ten;
                                                ht.DiaChi = kh[i].DiaChi;
                                                ht.PhatSinhCo = Format(double.Parse(item1.ThanhToan));
                                                ht.PhatSinhNo = "0";
                                                ht.GhiChu = item1.GhiChu;
                                                ht.TruongNgam = "PhieuTTCuaKH";
                                                ht.MaTruongNgam = item1.MaPhieuTTCuaKH;
                                                array.Add(ht);
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        #endregion

                        #region bán lẻ
                        //bán lẻ
                        foreach (Entities.HDBanLe item in buonle)
                        {
                            if (item.MaKho == makho && item.MaKhachHang == kh[i].MaKhachHang
                                 && item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc.Date)
                            {
                                phatsinhno += Convert.ToDouble(item.TongTienThanhToan);
                                string ngaylap = Common.Utilities.XuLy(2, item.NgayLap.ToShortDateString());
                                ht = new Entities.CongNo();
                                ht.NgayLap = ngaylap;
                                ht.MaDoiTuong = kh[i].MaKhachHang;
                                ht.TenDoiTuong = kh[i].Ten;
                                ht.DiaChi = kh[i].DiaChi;
                                ht.PhatSinhNo = Format(double.Parse(item.TongTienThanhToan));
                                ht.PhatSinhCo = "0";
                                ht.GhiChu = "";
                                ht.TruongNgam = "BanLe";
                                ht.MaTruongNgam = item.MaHDBanLe;
                                array.Add(ht);
                                foreach (Entities.ChiTietPhieuTTCuaKH item1 in chitietphieuttcuakh)
                                {

                                    if (item1.MaHDBanHang == item.MaHDBanLe)
                                    {
                                        foreach (Entities.PhieuTTCuaKH item2 in phieuttcuakh)
                                        {
                                            if (item2.MaPhieuTTCuaKH == item1.MaPhieuTTCuaKH && item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc.Date)
                                            {
                                                phatsinhco += double.Parse(item1.ThanhToan);
                                                ht = new Entities.CongNo();
                                                ht.NgayLap = item2.NgayLap.ToString("dd/MM/yyyy");
                                                ht.MaDoiTuong = kh[i].MaKhachHang;
                                                ht.TenDoiTuong = kh[i].Ten;
                                                ht.DiaChi = kh[i].DiaChi;
                                                ht.PhatSinhCo = Format(double.Parse(item1.ThanhToan));
                                                ht.PhatSinhNo = "0";
                                                ht.GhiChu = item1.GhiChu;
                                                ht.TruongNgam = "PhieuTTCuaKH";
                                                ht.MaTruongNgam = item1.MaPhieuTTCuaKH;
                                                array.Add(ht);
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        #endregion

                        #region khách hàng trả lại bán buôn
                        // khách hàng trả lại bán buôn
                        foreach (Entities.KhachHangTraLaiBanBuon item in khbb)
                        {
                            if (item.MaKho == makho && item.MaKhachHang == kh[i].MaKhachHang
                                 && item.NgayNhap.Date >= batdau.Date && item.NgayNhap.Date <= ketthuc.Date)
                            {
                                phatsinhco += Convert.ToDouble(item.TienBoiThuong);
                                string ngaylap = Common.Utilities.XuLy(2, item.NgayNhap.ToShortDateString());
                                ht = new Entities.CongNo();
                                ht.NgayLap = ngaylap;
                                ht.MaDoiTuong = kh[i].MaKhachHang;
                                ht.TenDoiTuong = kh[i].Ten;
                                ht.DiaChi = kh[i].DiaChi;
                                ht.PhatSinhCo = Format(double.Parse(item.TienBoiThuong));
                                ht.PhatSinhNo = "0";
                                ht.GhiChu = item.GhiChu;
                                ht.TruongNgam = "KhachHangTraLaiBanBuon";
                                ht.MaTruongNgam = item.MaKhachHangTraLaiBanBuon;
                                array.Add(ht);
                                foreach (Entities.ChiTietPhieuTTCuaKH item1 in chitietphieuttcuakh)
                                {

                                    if (item1.MaHDBanHang == item.MaKhachHangTraLaiBanBuon)
                                    {
                                        foreach (Entities.PhieuTTCuaKH item2 in phieuttcuakh)
                                        {
                                            if (item2.MaPhieuTTCuaKH == item1.MaPhieuTTCuaKH
                                                 && item.NgayNhap.Date >= batdau.Date && item.NgayNhap.Date <= ketthuc.Date)
                                            {
                                                if (item2.LoaiPhieu)
                                                {
                                                    phatsinhno += double.Parse(item1.ThanhToan);
                                                    ht = new Entities.CongNo();
                                                    ht.NgayLap = item2.NgayLap.ToString("dd/MM/yyyy");
                                                    ht.MaDoiTuong = kh[i].MaKhachHang;
                                                    ht.TenDoiTuong = kh[i].Ten;
                                                    ht.DiaChi = kh[i].DiaChi;
                                                    ht.PhatSinhNo = Format(double.Parse(item1.ThanhToan));
                                                    ht.PhatSinhCo = "0";
                                                    ht.GhiChu = item2.GhiChu;
                                                    ht.TruongNgam = "PhieuTTCuaKH";
                                                    ht.MaTruongNgam = item1.MaPhieuTTCuaKH;
                                                    array.Add(ht);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        #endregion

                        #region khách hàng trả lại bán lẻ
                        // khách hàng trả lại bán lẻ
                        foreach (Entities.KhachHangTraLaiBanLe item in khbl)
                        {
                            if (item.MaKho == makho && item.MaKhachHang == item.MaKhachHang
                                 && item.NgayNhap.Date >= batdau.Date && item.NgayNhap.Date <= ketthuc.Date)
                            {

                                phatsinhco += Convert.ToDouble(item.TienBoiThuong);
                                string ngaylap = Common.Utilities.XuLy(2, item.NgayNhap.ToShortDateString());
                                ht = new Entities.CongNo();
                                ht.NgayLap = ngaylap;
                                ht.MaDoiTuong = kh[i].MaKhachHang;
                                ht.TenDoiTuong = kh[i].Ten;
                                ht.DiaChi = kh[i].DiaChi;
                                ht.PhatSinhCo = Format(double.Parse(item.TienBoiThuong));
                                ht.PhatSinhNo = "0";
                                ht.GhiChu = item.GhiChu;
                                ht.TruongNgam = "KhachHangTraLaiBanLe";
                                ht.MaTruongNgam = item.MaKhachHangTraLaiBanLe;
                                array.Add(ht);
                                foreach (Entities.ChiTietPhieuTTCuaKH item1 in chitietphieuttcuakh)
                                {
                                    if (item1.MaHDBanHang == item.MaKhachHangTraLaiBanLe)
                                    {
                                        foreach (Entities.PhieuTTCuaKH item2 in phieuttcuakh)
                                        {
                                            if (item2.MaPhieuTTCuaKH == item1.MaPhieuTTCuaKH
                                                 && item.NgayNhap.Date >= batdau.Date && item.NgayNhap.Date <= ketthuc.Date)
                                            {
                                                if (item2.LoaiPhieu)
                                                {
                                                    phatsinhno += double.Parse(item1.ThanhToan);
                                                    ht = new Entities.CongNo();
                                                    ht.NgayLap = item2.NgayLap.ToString("dd/MM/yyyy");
                                                    ht.MaDoiTuong = kh[i].MaKhachHang;
                                                    ht.TenDoiTuong = kh[i].Ten;
                                                    ht.DiaChi = kh[i].DiaChi;
                                                    ht.PhatSinhNo = Format(double.Parse(item1.ThanhToan));
                                                    ht.PhatSinhCo = "0";
                                                    ht.GhiChu = item2.GhiChu;
                                                    ht.TruongNgam = "PhieuTTCuaKH";
                                                    ht.MaTruongNgam = item1.MaPhieuTTCuaKH;
                                                    array.Add(ht);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        #endregion

                        sdck = sddk + phatsinhno - phatsinhco;
                        mangchitiet[soluongkh] = new Entities.SoDuCongNo();
                        mangchitiet[soluongkh].HanhDong = "UpdateTrangThai";
                        mangchitiet[soluongkh].MaSoDuCongNo = maSoDuCongNo;
                        mangchitiet[soluongkh].MaDoiTuong = kh[i].MaKhachHang;
                        mangchitiet[soluongkh].TenDoiTuong = kh[i].Ten;
                        mangchitiet[soluongkh].DiaChi = kh[i].DiaChi;
                        mangchitiet[soluongkh].SoDuCuoiKy = Format(sdck);
                        mangchitiet[soluongkh].NgayKetChuyen = date;
                        mangchitiet[soluongkh].TrangThai = false;

                        congnokh[soluongkh] = new Entities.CongNo();
                        congnokh[soluongkh].MaDoiTuong = kh[i].MaKhachHang;
                        congnokh[soluongkh].TenDoiTuong = kh[i].Ten;
                        congnokh[soluongkh].DiaChi = kh[i].DiaChi;
                        congnokh[soluongkh].DuDauKy = Format(sddk);
                        congnokh[soluongkh].PhatSinhNo = Format(phatsinhno);
                        congnokh[soluongkh].PhatSinhCo = Format(phatsinhco);
                        congnokh[soluongkh].DuCuoiKy = Format(sdck);

                        soluongkh++;
                    }
                    phatsinhno = phatsinhco = 0;

                    // tính toán của mảng khách hàng
                    if (congnokh.Length == 1)
                    {
                        for (int i = 0; i < congnokh.Length; i++)
                        {
                            sdck = sddk + phatsinhno - phatsinhco;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < congnokh.Length - 1; i++)
                        {
                            sddk += Convert.ToDouble(congnokh[i].DuDauKy);
                            phatsinhco += Convert.ToDouble(congnokh[i].PhatSinhCo);
                            phatsinhno += Convert.ToDouble(congnokh[i].PhatSinhNo);
                            sdck = sddk + phatsinhno - phatsinhco;
                        }
                    }
                    congnokh[congnokh.Length - 1] = new Entities.CongNo();
                    congnokh[congnokh.Length - 1].DiaChi = "Tổng cộng";
                    congnokh[congnokh.Length - 1].DuDauKy = Format(sddk);
                    congnokh[congnokh.Length - 1].PhatSinhNo = Format(phatsinhno);
                    congnokh[congnokh.Length - 1].PhatSinhCo = Format(phatsinhco);
                    congnokh[congnokh.Length - 1].DuCuoiKy = Format(sdck);
                    #endregion
                }
                else
                {
                    #region nhà cung cấp
                    hienthi1 = HienThi(makho);
                    // Nhà Cung Cấp
                    for (int i = 0; i < ncc.Length; i++)
                    {
                        phatsinhco = 0;
                        phatsinhno = 0;
                        sddk = 0;
                        string maSoDuCongNo = "";
                        foreach (Entities.SoDuCongNo item in hienthi1)
                        {
                            if (item.MaDoiTuong == ncc[i].MaNhaCungCap
                                && batdau.Month == item.NgayKetChuyen.Month && batdau.Year == item.NgayKetChuyen.Year)
                            {
                                maSoDuCongNo = item.MaSoDuCongNo;
                                sddk = Double.Parse(item.SoDuDauKy);
                                break;
                            }

                        }

                        #region hóa đơn nhập
                        // hóa đơn nhập
                        foreach (Entities.HoaDonNhap item in hdn)
                        {
                            if (item.MaKho == makho && ncc[i].MaNhaCungCap == item.MaNhaCungCap
                                 && item.NgayNhap.Date >= batdau.Date && item.NgayNhap.Date <= ketthuc.Date)
                            {
                                phatsinhco += Convert.ToDouble(item.TongTien);
                                string ngaylap = Common.Utilities.XuLy(2, item.NgayNhap.ToShortDateString());
                                ht = new Entities.CongNo(ngaylap, item.MaHoaDonNhap, ncc[i].TenNhaCungCap, ncc[i].DiaChi, "0", Format(double.Parse(item.TongTien)), item.GhiChu, "HoaDonNhap");
                                ht.MaTruongNgam = ht.MaDoiTuong;
                                ht.MaDoiTuong = ncc[i].MaNhaCungCap;
                                array.Add(ht);
                                foreach (Entities.ChiTietPhieuTTNCC item1 in chitietphieuttncc)
                                {
                                    if (item.MaHoaDonNhap == item1.MaHoaDonNhap)
                                    {
                                        bool kt = false;
                                        foreach (Entities.PhieuTTNCC item2 in phieuttncc)
                                        {
                                            if (item1.MaPhieuTTNCC == item2.MaPhieuTTNCC
                                                 && item.NgayNhap.Date >= batdau.Date && item.NgayNhap.Date <= ketthuc.Date)
                                            {
                                                if (item2.LoaiPhieu)
                                                {
                                                    phatsinhno += double.Parse(item1.ThanhToan);
                                                    ht = new Entities.CongNo(item2.NgayLap.ToString("dd/MM/yyyy"), item2.MaPhieuTTNCC, ncc[i].TenNhaCungCap, ncc[i].DiaChi, Format(double.Parse(item1.ThanhToan)), "0", item2.GhiChu, "PhieuTTNCC");
                                                    ht.MaTruongNgam = ht.MaDoiTuong;
                                                    ht.MaDoiTuong = ncc[i].MaNhaCungCap;
                                                    array.Add(ht);
                                                    kt = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                }


                            }
                        }
                        #endregion

                        #region trả lại nhà cung cấp
                        // trả lại nhà cung cấp
                        foreach (Entities.TraLaiNCC item in tl)
                        {
                            if (item.MaKho == makho && item.MaNCC == ncc[i].MaNhaCungCap
                                 && item.Ngaytra.Date >= batdau.Date && item.Ngaytra.Date <= ketthuc.Date)
                            {

                                phatsinhno += Convert.ToDouble(item.TienBoiThuong);
                                string ngaylap = Common.Utilities.XuLy(2, item.Ngaytra.ToShortDateString());
                                ht = new Entities.CongNo(ngaylap, item.MaHDTraLaiNCC, ncc[i].TenNhaCungCap, ncc[i].DiaChi, Format(double.Parse(item.TienBoiThuong)), "0", item.GhiChu, "TLNCC");
                                ht.MaTruongNgam = ht.MaDoiTuong;
                                ht.MaDoiTuong = ncc[i].MaNhaCungCap;
                                array.Add(ht);
                                foreach (Entities.ChiTietPhieuTTNCC item1 in chitietphieuttncc)
                                {
                                    if (item.MaHDTraLaiNCC == item1.MaHoaDonNhap)
                                    {
                                        bool kt = false;
                                        foreach (Entities.PhieuTTNCC item2 in phieuttncc)
                                        {
                                            if (item1.MaPhieuTTNCC == item2.MaPhieuTTNCC
                                                 && item.Ngaytra.Date >= batdau.Date && item.Ngaytra.Date <= ketthuc.Date)
                                            {
                                                if (!item2.LoaiPhieu)
                                                {
                                                    phatsinhco += double.Parse(item1.ThanhToan);
                                                    ht = new Entities.CongNo(item2.NgayLap.ToString("dd/MM/yyyy"), item2.MaPhieuTTNCC, ncc[i].TenNhaCungCap, ncc[i].DiaChi, "0", Format(double.Parse(item1.ThanhToan)), item2.GhiChu, "PhieuTTNCC");
                                                    ht.MaTruongNgam = ht.MaDoiTuong;
                                                    ht.MaDoiTuong = ncc[i].MaNhaCungCap;
                                                    array.Add(ht);
                                                    kt = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                }
                            }
                        }

                        #endregion

                        sdck = sddk + phatsinhco - phatsinhno;
                        mangchitiet[soluongncc] = new Entities.SoDuCongNo("UpdateTrangThai", maSoDuCongNo, ncc[i].MaNhaCungCap, ncc[i].TenNhaCungCap, ncc[i].DiaChi, Format(sdck), date, true);
                        congnoncc[soluongncc] = new Entities.CongNo(ncc[i].MaNhaCungCap, ncc[i].TenNhaCungCap, ncc[i].DiaChi, Format(sddk), Format(phatsinhno), Format(phatsinhco), Format(sdck));
                        soluongncc++;

                    }
                    phatsinhno = phatsinhco = 0;
                    // tính toán của mảng nhà cung cấp
                    if (congnoncc.Length == 1)
                    {
                        for (int i = 0; i < congnoncc.Length; i++)
                        {
                            sdck = sddk + phatsinhco - phatsinhno;
                            congnoncc[congnoncc.Length - 1] = new Entities.CongNo("Tổng cộng", Format(sddk), Format(phatsinhno), Format(phatsinhco), Format(sdck));
                        }
                    }
                    else
                    {
                        for (int i = 0; i < congnoncc.Length - 1; i++)
                        {
                            sddk += Convert.ToDouble(congnoncc[i].DuDauKy);
                            phatsinhco += Convert.ToDouble(congnoncc[i].PhatSinhCo);
                            phatsinhno += Convert.ToDouble(congnoncc[i].PhatSinhNo);
                            sdck = sddk + phatsinhco - phatsinhno;
                            congnoncc[congnoncc.Length - 1] = new Entities.CongNo("Tổng Cộng", Format(sddk), Format(phatsinhno), Format(phatsinhco), Format(sdck));
                        }
                    }
                    #endregion
                }

                if (cbbDoiTuong.SelectedIndex == 0)
                {
                    soluong = 0;
                    int sl = 0;
                    Entities.CongNo[] sdcn = new Entities.CongNo[congnokh.Length];
                    for (int i = 0; i < congnokh.Length - 1; i++)
                    {
                        double dauky = Convert.ToDouble(congnokh[i].DuDauKy);
                        double cuoiky = Convert.ToDouble(congnokh[i].DuCuoiKy);
                        double psco = Convert.ToDouble(congnokh[i].PhatSinhCo);
                        double psno = Convert.ToDouble(congnokh[i].PhatSinhNo);
                        if (dauky == 0 && cuoiky == 0 && psco == 0 && psno == 0)
                        {
                            soluong++;
                        }
                        else
                        {
                            sdcn[sl] = congnokh[i];
                            sl++;
                        }
                    }
                    Entities.CongNo[] sdcn1 = new Entities.CongNo[sl + 1];
                    for (int i = 0; i < sdcn1.Length; i++)
                    {
                        if (i < sdcn1.Length - 1)
                            sdcn1[i] = sdcn[i];
                        else
                            sdcn1[i] = congnokh[congnokh.Length - 1];

                    }
                    dgvHienThi.DataSource = sdcn1;
                    fix();
                }
                else
                {
                    soluong = 0;
                    int sl = 0;
                    Entities.CongNo[] sdcn = new Entities.CongNo[congnoncc.Length];
                    for (int i = 0; i < congnoncc.Length - 1; i++)
                    {
                        double dauky = Convert.ToDouble(congnoncc[i].DuDauKy);
                        double cuoiky = Convert.ToDouble(congnoncc[i].DuCuoiKy);
                        double psco = Convert.ToDouble(congnoncc[i].PhatSinhCo);
                        double psno = Convert.ToDouble(congnoncc[i].PhatSinhNo);
                        if (dauky == 0 && cuoiky == 0 && psco == 0 && psno == 0)
                        {
                            soluong++;
                        }
                        else
                        {
                            sdcn[sl] = congnoncc[i];
                            sl++;
                        }
                    }
                    Entities.CongNo[] sdcn1 = new Entities.CongNo[sl + 1];
                    for (int i = 0; i < sdcn1.Length; i++)
                    {
                        if (i < sdcn1.Length - 1)
                            sdcn1[i] = sdcn[i];
                        else
                            sdcn1[i] = congnoncc[congnoncc.Length - 1];
                        a = congnoncc[0].DuCuoiKy;
                    }
                    dgvHienThi.DataSource = sdcn1;
                    fix();
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Chi tiết theo khách hàng

        Entities.HDBanBuon[] bb1;
        Entities.HDBanLe[] bl1;
        Entities.CongNo[] congnochitietkh;
        Entities.KhachHangTraLaiBanBuon[] khbb1;
        Entities.KhachHangTraLaiBanLe[] khbl1;
        Entities.KhachHang kh1;
        Entities.PhieuTTCuaKH[] phieuttcuakh1;
        Entities.ChiTietPhieuTTCuaKH[] chitietphieuttcuakh1;
        Entities.CongNo[] congnokh1;
        public void ChiTietTheoKhachHang(string maKhachHang)
        {
            try
            {
                congnokh1 = new Entities.CongNo[2];
                int sotang = 0;
                phatsinhco = 0;
                phatsinhno = 0;
                sdck = sddk = 0;
                phatsinhco = 0;
                phatsinhno = 0;
                //// lấy thông tin của khách hàng
                string tenkh = "";
                string diachi = "";
                for (int j = 0; j < kh.Length; j++)
                {
                    if (kh[j].MaKhachHang == maKhachHang)
                    {
                        kh1 = kh[j];
                        tenkh = kh[j].Ten;
                        diachi = kh[j].DiaChi;
                        break;
                    }
                }

                ArrayList temp = new ArrayList();
                foreach (Entities.CongNo item in (Entities.CongNo[])array.ToArray(typeof(Entities.CongNo)))
                {
                    Entities.CongNo cn = item;
                    if (cn.MaDoiTuong == maKhachHang)
                    {
                        if (cn.TruongNgam == "BanBuon")
                        {
                            bb1 = (Entities.HDBanBuon[])bb.Where(p => p.MaHDBanBuon == cn.MaTruongNgam).ToArray();
                        }
                        if (cn.TruongNgam == "BanLe")
                        {
                            bl1 = (Entities.HDBanLe[])buonle.Where(p => p.MaHDBanLe == cn.MaTruongNgam).ToArray();
                        }
                        if (cn.TruongNgam == "KhachHangTraLaiBanBuon")
                        {
                            khbb1 = (Entities.KhachHangTraLaiBanBuon[])khbb.Where(p => p.MaKhachHangTraLaiBanBuon == cn.MaTruongNgam).ToArray();
                        }
                        if (cn.TruongNgam == "KhachHangTraLaiBanLe")
                        {
                            khbl1 = (Entities.KhachHangTraLaiBanLe[])khbl.Where(p => p.MaKhachHangTraLaiBanLe == cn.MaTruongNgam).ToArray();
                        }
                        if (cn.TruongNgam == "PhieuTTCuaKH")
                        {
                            phieuttcuakh1 = (Entities.PhieuTTCuaKH[])phieuttcuakh.Where(p => p.MaPhieuTTCuaKH == cn.MaTruongNgam).ToArray();
                        }
                        cn.Ton = "0";
                        temp.Add(cn);
                    }
                }
                congnochitietkh = new Entities.CongNo[temp.Count + 1];


                foreach (Entities.CongNo item in (Entities.CongNo[])temp.ToArray(typeof(Entities.CongNo)))
                {
                    congnochitietkh[sotang] = item;
                    sotang++;
                }
                phatsinhno = phatsinhco = 0;
                // tính toán của mảng khách hàng
                for (int j = 0; j < congnochitietkh.Length - 1; j++)
                {
                    phatsinhco += Convert.ToDouble(congnochitietkh[j].PhatSinhCo);
                    phatsinhno += Convert.ToDouble(congnochitietkh[j].PhatSinhNo);
                    sdck = sddk + phatsinhno - phatsinhco;
                    congnochitietkh[congnochitietkh.Length - 1] = new Entities.CongNo("Tổng cộng", Format(sddk), Format(phatsinhno), Format(phatsinhco), Format(sdck));
                }

            }
            catch
            {
            }
        }
        #endregion

        #region chi tiết nhà cung cấp
        Entities.HoaDonNhap[] hdn2;
        Entities.TraLaiNCC[] tlncc2;
        Entities.CongNo[] congnoncc2;
        Entities.CongNo[] congnochitietncc2;
        Entities.PhieuTTNCC[] phieuttncc2;
        Entities.ChiTietPhieuTTNCC[] chitietphieuttncc2;
        Entities.NhaCungCap ncc2;
        public void ChiTietNhaCungCap(string maNhaCungCap)
        {
            try
            {
                congnoncc2 = new Entities.CongNo[2];
                int sotang = 0;
                phatsinhco = 0;
                phatsinhno = 0;
                sdck = sddk = 0;
                phatsinhco = 0;
                phatsinhno = 0;

                string tenncc = "";
                string diachi = "";
                for (int j = 0; j < ncc.Length; j++)
                {
                    if (ncc[j].MaNhaCungCap == maNhaCungCap)
                    {
                        ncc2 = ncc[j];
                        tenncc = ncc[j].TenNhaCungCap;
                        diachi = ncc[j].DiaChi;
                        break;
                    }
                }

                ArrayList temp = new ArrayList();
                ArrayList hoadn = new ArrayList();
                ArrayList tralncc = new ArrayList();
                ArrayList ptncc = new ArrayList();
                foreach (Entities.CongNo item in (Entities.CongNo[])array.ToArray(typeof(Entities.CongNo)))
                {
                    Entities.CongNo cn = item;
                    if (cn.MaDoiTuong == maNhaCungCap)
                    {
                        if (cn.TruongNgam == "HoaDonNhap")
                        {
                            hdn2 = (Entities.HoaDonNhap[])hdn.Where(p => p.MaHoaDonNhap == cn.MaTruongNgam).ToArray();
                        }
                        if (cn.TruongNgam == "TLNCC")
                        {
                            tlncc2 = (Entities.TraLaiNCC[])tl.Where(p => p.MaHDTraLaiNCC == cn.MaTruongNgam).ToArray();
                        }
                        if (cn.TruongNgam == "PhieuTTNCC")
                        {
                            phieuttncc2 = (Entities.PhieuTTNCC[])phieuttncc.Where(p => p.MaPhieuTTNCC == cn.MaTruongNgam).ToArray();
                        }
                        cn.Ton = "0";
                        temp.Add(cn);
                    }
                }
                congnochitietncc2 = new Entities.CongNo[temp.Count + 1];
                foreach (Entities.CongNo item in (Entities.CongNo[])temp.ToArray(typeof(Entities.CongNo)))
                {
                    congnochitietncc2[sotang] = item;
                    sotang++;
                }
                phatsinhno = phatsinhco = 0;
                // tính toán của mảng nhà cung cấp
                for (int j = 0; j < congnochitietncc2.Length - 1; j++)
                {
                    phatsinhco += Convert.ToDouble(congnochitietncc2[j].PhatSinhCo);
                    phatsinhno += Convert.ToDouble(congnochitietncc2[j].PhatSinhNo);
                    sdck = sddk + phatsinhco - phatsinhno;
                    congnochitietncc2[congnochitietncc2.Length - 1] = new Entities.CongNo("Tổng cộng", Format(sddk), Format(phatsinhno), Format(phatsinhco), Format(sdck));
                }
            }
            catch
            {
            }
        }
        #endregion

        #region lọc điều kiện
        void LocDieuKien()
        {
            try
            {
                GUI.LocDieuKien.frmLocDieuKien frm = new LocDieuKien.frmLocDieuKien();
                frm.ShowDialog();
                if (GUI.LocDieuKien.frmLocDieuKien.kiemtra)
                {
                    DateTime truoc1;
                    DateTime sau1;
                    try
                    {
                        truoc1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.truoc);
                        sau1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.sau);
                    }
                    catch
                    {
                        return;
                    }
                    batdau = truoc1;
                    ketthuc = sau1;
                    HienThiTong();
                    lblTieuDe.Text = "Công Nợ từ ngày " + batdau.ToString("dd/MM/yyyy") + " đến ngày " + ketthuc.ToString("dd/MM/yyyy");
                    GUI.LocDieuKien.frmLocDieuKien.kiemtra = false;
                }

            }
            catch
            {
            }
        }
        #endregion

        #region xử lý form
        public string Format(double a)
        {
            if (a >= 0 && a < 10)
                return a.ToString();
            if (a < 0)
                return String.Format("{0,-0:0,0}", a);
            else
                return String.Format("{0:0,0}", a);
        }
        #endregion

        #region Event
        private void frmCongNo_Load(object sender, EventArgs e)
        {
            try
            {
                dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
                dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
                dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
                btnChiTiet.Click += new EventHandler(btnChiTiet_Click);
                btnThoat.Click += new EventHandler(btnThoat_Click);
                this.FormClosing += new FormClosingEventHandler(frmCongNo_FormClosing);
                cbbDoiTuong.SelectedIndexChanged += new EventHandler(cbb_SelectedIndexChanged); this.WindowState = FormWindowState.Maximized;
            }
            catch
            {
            }
        }

        void cbb_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                month = cbbThang.Text;
                year = cbbNam.Text;
                if (cbbNam.Text != "")
                {
                    if (cbbThang.Text != "")
                    {
                        if (cbbDoiTuong.Text != "" && year != "" && month != "")
                        {
                            lblTieuDe.Text = "Công Nợ từ ngày " + batdau.ToString("dd/MM/yyyy") + " đến ngày " + ketthuc.ToString("dd/MM/yyyy");
                            Entities.CaiDatKho kho = Entitystatic.ViewFile();
                            makho = kho.MaKho;
                            tenkho = kho.TenKho;
                            if (makho.Length == 0)
                            {
                                MessageBox.Show(Common.MSG.MSG28(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                                return;
                            }
                            batdau = DateTime.Parse(cbbThang.Text + "/01/" + cbbNam.Text);
                            if (cbbThang.Text == "12")
                                ketthuc = DateTime.Parse("01/01/" + (Convert.ToInt32(cbbNam.Text) + 1).ToString());
                            else
                                ketthuc = DateTime.Parse((Convert.ToInt32(cbbThang.Text) + 1).ToString() + "/01/" + cbbNam.Text);

                            HienThiTong();
                        }
                    }
                }
            }
            catch { }
        }

        void btnChiTiet_Click(object sender, EventArgs e)
        {
            ChiTietGiaoDich();
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                ChiTietGiaoDich();
            }
        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            ChiTietGiaoDich();
        }

        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        private void frmCongNo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }
        #endregion

        private void btnLocDieuKien_Click(object sender, EventArgs e)
        {
            LocDieuKien();
        }

        private void cbbThang_SelectedIndexChanged(object sender, EventArgs e)
        {

            //month = cbbThang.Text;
            //year = cbbNam.Text;
            //batdau = DateTime.Parse(cbbThang.Text + "/01/" + cbbNam.Text);
            //if (cbbThang.Text == "12")
            //    ketthuc = DateTime.Parse("01/01/" + (Convert.ToInt32(cbbNam.Text) + 1).ToString());
            //else
            //    ketthuc = DateTime.Parse((Convert.ToInt32(cbbThang.Text) + 1).ToString() + "/01/" + cbbNam.Text);
            //lblTieuDe.Text = "Công Nợ từ ngày " + batdau.ToString("dd/MM/yyyy") + " đến ngày " + ketthuc.ToString("dd/MM/yyyy");

            //HienThiTong();
        }

        private void cbbNam_SelectedIndexChanged(object sender, EventArgs e)
        {

            //month = cbbThang.Text;
            //year = cbbNam.Text;
            //batdau = DateTime.Parse(cbbThang.Text + "/01/" + cbbNam.Text);
            //if (cbbThang.Text == "12")
            //    ketthuc = DateTime.Parse("01/01/" + (Convert.ToInt32(cbbNam.Text) + 1).ToString());
            //else
            //    ketthuc = DateTime.Parse((Convert.ToInt32(cbbThang.Text) + 1).ToString() + "/01/" + cbbNam.Text);
            //lblTieuDe.Text = "Công Nợ từ ngày " + batdau.ToString("dd/MM/yyyy") + " đến ngày " + ketthuc.ToString("dd/MM/yyyy");

            //HienThiTong();
        }
    }
}
#endregion